import InvocationHandler from '@/assets/js/mapboxgl/newDouble/InvocationHandler';

export default class Proxy {

    constructor(master, slave) {
        
        this.masterService = master // 实例化地图基础操作类
        this.slaveService = slave
        this.obj = this.newProxyInstance(this.masterService, new InvocationHandler());
        this.s = this.s.bind(this);
        this.r = this.r.bind(this);
        this.s1 = this.s1.bind(this);
        this.r1 = this.r1.bind(this);
        //this.n();
        //this.n1();
        
    }

    newProxyInstance(target, handler) {
        const obj = new Object();
        const names = Object.getOwnPropertyNames(target.__proto__);
        for (let i in names) {
            if (names[i] == "constructor") continue;
            obj[names[i]] = function () {
                return handler.invoke(this.masterService, this.slaveService, names[i], arguments);
            }.bind(this);
        }
        return obj;
    }

    getMasterService() {
        return this.obj;
    }

    o(a, b) {
        a.setDrawControlFC(b.getDrawControlFC());
        a.setChangeMode("simple_select")
        b.setChangeMode("simple_select")
    }

    n() {
        this.masterService.on("map.create", this.s), this.slaveService.on("map.create", this.r)
        this.masterService.on("map.update", this.s), this.slaveService.on("map.update", this.r)
        this.masterService.on("map.delete", this.s), this.slaveService.on("map.delete", this.r)
    }

    i() {
        this.masterService.off("map.create", this.s), this.slaveService.off("map.create", this.r)
        this.masterService.off("map.update", this.s), this.slaveService.off("map.update", this.r)
        this.masterService.off("map.delete", this.s), this.slaveService.off("map.delete", this.r)
    }

    s() {
        
        this.i();  //Off
        this.o(this.slaveService, this.masterService);
        this.n()   //on
    }

    r() {
        this.i();
        this.o(this.masterService, this.slaveService);
        this.n()
    }

    o1(a, b) {
        a.getPop().remove();
        b.getPop().remove();
        //a.setHighLightFilter()
    }

    n1() {
        this.masterService.getPop().on("close", this.s1), this.slaveService.getPop().on("close", this.r1);
    }

    i1() {
        this.masterService.getPop().off("close", this.s1), this.slaveService.getPop().off("close", this.r1);
    }

    s1() {
        this.i1();
        this.o1(this.slaveService, this.masterService);
        this.n1()
    }

    r1() {
        this.i1();
        this.o1(this.masterService, this.slaveService);
        this.n1()
    }
}